package com.inoneone.youba.mcht.controller;

import com.inoneone.toolkit.exception.AssertUtil;
import com.inoneone.toolkit.exception.BusinessRuntimeException;
import com.inoneone.toolkit.exception.RegExp;
import com.inoneone.youba.mcht.dao.po.Card;
import com.inoneone.youba.mcht.dao.po.Merchant;
import com.inoneone.youba.mcht.dao.po.Tanker;
import com.inoneone.youba.mcht.dto.BaseRsp;
import com.inoneone.youba.mcht.dto.req.*;
import com.inoneone.youba.mcht.dto.rsp.MchChargeStatisticsRsp;
import com.inoneone.youba.mcht.dto.rsp.MchFuelStatisticsRsp;
import com.inoneone.youba.mcht.enums.ErrorCode;
import com.inoneone.youba.mcht.service.CardApi;
import com.inoneone.youba.mcht.service.DataApi;
import com.inoneone.youba.mcht.service.MerchantApi;
import com.inoneone.youba.mcht.util.CommonUtil;
import com.inoneone.youba.mcht.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping(value = "/data", method = {RequestMethod.POST})
public class DataController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DataApi dataApi;

    @Autowired
    private MerchantApi merchantApi;

    @Autowired
    private CardApi cardApi;


    /**
     * Description: pc端最近1个月加油数据</br>
     * Date: 2017-06-20 04:06:10</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
     */
    @PostMapping(value = "/latestFuel")
    @ResponseBody
    public Object latestFuel(HttpSession httpSession) {
        //参数检查

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //获取日期
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        //结束时间
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date endDate = calendar.getTime();

        //开始时间
        calendar.add(Calendar.MONTH, -1);
        Date startDate = calendar.getTime();

        //获取系统信息
        MchFuelStatisticsRsp mchFuelStatisticsRsp = dataApi.queryMchData(merchant.getId(), startDate, endDate, false);

        //返回
        return new BaseRsp(mchFuelStatisticsRsp);
    }

    /**
     * Description: 商户维度加油数据</br>
     * Date: 2017-06-22 17:24:32</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
     */
    @PostMapping(value = "/mchFuel")
    @ResponseBody
    public Object mchFuel(HttpSession httpSession, @RequestBody MchFuelReq mchFuelReq) {
        //参数检查
        AssertUtil.isMatch(mchFuelReq.getBeginDate(), RegExp.YYYYMMDD, ErrorCode.DATE);
        AssertUtil.isMatch(mchFuelReq.getEndDate(), RegExp.YYYYMMDD, ErrorCode.DATE);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //结束时间
        Date endDate = DateUtil.parseDate(mchFuelReq.getEndDate(), "yyyy-MM-dd");

        //开始时间
        Date startDate = DateUtil.parseDate(mchFuelReq.getBeginDate(), "yyyy-MM-dd");


        //获取系统信息
        MchFuelStatisticsRsp mchFuelStatisticsRsp = dataApi.queryMchData(merchant.getId(), startDate, endDate, false);

        //返回
        return new BaseRsp(mchFuelStatisticsRsp);
    }


    public Object mchFuelMonth(HttpSession httpSession, @RequestBody MchFuelPlusReq mchFuelPlusReq) {
        //参数检查
        AssertUtil.isMatch(mchFuelPlusReq.getMonth(), RegExp.YYYYMM, ErrorCode.DATE);


        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //年月
        Date month = DateUtil.parseDate(mchFuelPlusReq.getMonth(), "yyyy-MM");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        //开始时间
        Date startDate = calendar.getTime();

        //结束时间
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.SECOND, -1);
        Date endDate = calendar.getTime();


        //获取系统信息
        MchFuelStatisticsRsp mchFuelStatisticsRsp = dataApi.queryMchData(merchant.getId(), startDate, endDate, true);

        //返回
        return new BaseRsp(mchFuelStatisticsRsp);
    }

    /**
     * Description: 加油机维度查询数据</br>
     * Date: 2017-06-22 17:24:56</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
     */
    @PostMapping(value = "/tankerFuel")
    @ResponseBody
    public Object tankerFuel(HttpSession httpSession, @RequestBody TankerFuelReq tankerFuelReq) {
        //参数检查
        AssertUtil.isMatch(tankerFuelReq.getTankerId(), RegExp.TANKER_ID, ErrorCode.TANKER_ID);
        AssertUtil.isMatch(tankerFuelReq.getBeginDate(), RegExp.YYYYMMDD, ErrorCode.DATE);
        AssertUtil.isMatch(tankerFuelReq.getEndDate(), RegExp.YYYYMMDD, ErrorCode.DATE);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //校验加油机是否属于登录商户
        Tanker tanker = merchantApi.queryTanker(tankerFuelReq.getTankerId());

        //结束时间
        Date endDate = DateUtil.parseDate(tankerFuelReq.getEndDate(), "yyyy-MM-dd");

        //开始时间
        Date startDate = DateUtil.parseDate(tankerFuelReq.getBeginDate(), "yyyy-MM-dd");

        //查询数据
        MchFuelStatisticsRsp mchFuelStatisticsRsp = dataApi.queryTankerData(tankerFuelReq.getTankerId(), startDate, endDate, false);

        //返回
        return new BaseRsp(mchFuelStatisticsRsp);
    }


    public Object tankerFuelMonth(HttpSession httpSession, @RequestBody TankerFuelMonthReq tankerFuelMonthReq) {

        //参数检查
        AssertUtil.isMatch(tankerFuelMonthReq.getTankerId(), RegExp.TANKER_ID, ErrorCode.TANKER_ID);
        AssertUtil.isMatch(tankerFuelMonthReq.getMonth(), RegExp.YYYYMM, ErrorCode.DATE);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //校验加油机是否属于登录商户
        Tanker tanker = merchantApi.queryTanker(tankerFuelMonthReq.getTankerId());

        //年月
        Date month = DateUtil.parseDate(tankerFuelMonthReq.getMonth(), "yyyy-MM");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        //开始时间
        Date startDate = calendar.getTime();

        //结束时间
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.SECOND, -1);
        Date endDate = calendar.getTime();

        //查询数据
        MchFuelStatisticsRsp mchFuelStatisticsRsp = dataApi.queryTankerData(tankerFuelMonthReq.getTankerId(), startDate, endDate, true);

        logger.debug("开始时间：" + startDate.toString());
        logger.debug("结束时间：" + endDate.toString());


        //返回
        return new BaseRsp(mchFuelStatisticsRsp);
    }

    /**
     * Description: ic卡维度加油数据</br>
     * Date: 2017-06-22 17:25:23</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
     */
    @PostMapping(value = "/cardFuel")
    @ResponseBody
    public Object cardFuel(HttpSession httpSession, @RequestBody CardFuelReq cardFuelReq) {

        //参数检查
        AssertUtil.isMatch(cardFuelReq.getCardId(), RegExp.CARD_NO, ErrorCode.CARD);
        AssertUtil.isMatch(cardFuelReq.getBeginDate(), RegExp.YYYYMMDD, ErrorCode.DATE);
        AssertUtil.isMatch(cardFuelReq.getEndDate(), RegExp.YYYYMMDD, ErrorCode.DATE);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //校验ic卡是否属于登录商户
        Card card = cardApi.queryCard(cardFuelReq.getCardId(), merchant.getId());

        //结束时间
        Date endDate = DateUtil.parseDate(cardFuelReq.getEndDate(), "yyyy-MM-dd");

        //开始时间
        Date startDate = DateUtil.parseDate(cardFuelReq.getBeginDate(), "yyyy-MM-dd");

        //获取系统信息
        MchFuelStatisticsRsp mchFuelStatisticsRsp = dataApi.queryCardData(cardFuelReq.getCardId(), startDate, endDate, false);

        //返回
        return new BaseRsp(mchFuelStatisticsRsp);
    }


    public Object cardFuelMonth(HttpSession httpSession, @RequestBody CardFuelMonthReq cardFuelMonthReq) {

        //参数检查
        AssertUtil.isMatch(cardFuelMonthReq.getCardId(), RegExp.CARD_NO, ErrorCode.CARD);
        AssertUtil.isMatch(cardFuelMonthReq.getMonth(), RegExp.YYYYMM, ErrorCode.DATE);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //校验ic卡是否属于登录商户
        Card card = cardApi.queryCard(cardFuelMonthReq.getCardId(), merchant.getId());

        //年月
        Date month = DateUtil.parseDate(cardFuelMonthReq.getMonth(), "yyyy-MM");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        //开始时间
        Date startDate = calendar.getTime();

        //结束时间
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.SECOND, -1);
        Date endDate = calendar.getTime();

        //获取系统信息
        MchFuelStatisticsRsp mchFuelStatisticsRsp = dataApi.queryCardData(cardFuelMonthReq.getCardId(), startDate, endDate, true);

        //返回
        return new BaseRsp(mchFuelStatisticsRsp);
    }

    /**
     * Description: 任意天ic卡充值数据查询</br>
     * Date: 2017-06-22 17:25:23</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
     */
    @PostMapping(value = "/cardCharge")
    @ResponseBody
    public Object cardCharge(HttpSession httpSession, @RequestBody CardChargeReq cardChargeReq) {

        //参数检查
        if (!CommonUtil.isNullOrEmpty(cardChargeReq.getCardId())) {
            AssertUtil.isMatch(cardChargeReq.getCardId(), RegExp.CARD_NO, ErrorCode.CARD);

        }
        AssertUtil.isMatch(cardChargeReq.getBeginDate(), RegExp.YYYYMMDD, ErrorCode.DATE);
        AssertUtil.isMatch(cardChargeReq.getEndDate(), RegExp.YYYYMMDD, ErrorCode.DATE);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        if (!CommonUtil.isNullOrEmpty(cardChargeReq.getCardId())) {
            //校验ic卡是否属于登录商户
            Card card = cardApi.queryCard(cardChargeReq.getCardId(), merchant.getId());
        }

        //结束时间
        Date endDate = DateUtil.parseDate(cardChargeReq.getEndDate(), "yyyy-MM-dd");

        //开始时间
        Date startDate = DateUtil.parseDate(cardChargeReq.getBeginDate(), "yyyy-MM-dd");

        //获取系统信息
        MchChargeStatisticsRsp mchChargeStatisticsRsp;
        if (!CommonUtil.isNullOrEmpty(cardChargeReq.getCardId())) {
            mchChargeStatisticsRsp = dataApi.queryChargeData(cardChargeReq.getCardId(), startDate, endDate, false);
        } else {
            mchChargeStatisticsRsp = dataApi.queryMchChargeData(merchant.getId(), startDate, endDate, false);
        }
        //返回
        return new BaseRsp(mchChargeStatisticsRsp);
    }

    /**
     * Description: 按月加油数据查询</br>
     * Date: 2017-09-14 16:04:56</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    @PostMapping(value = "/cardChargePlus")
    @ResponseBody
    public Object cardChargePlus(HttpSession httpSession, @RequestBody CardChargePlusReq cardChargePlusReq) {

        //参数检查
        if (!CommonUtil.isNullOrEmpty(cardChargePlusReq.getCardId())) {
            AssertUtil.isMatch(cardChargePlusReq.getCardId(), RegExp.CARD_NO, ErrorCode.CARD);
        }
        AssertUtil.isMatch(cardChargePlusReq.getMonth(), RegExp.YYYYMM, ErrorCode.DATE);

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        if (!CommonUtil.isNullOrEmpty(cardChargePlusReq.getCardId())) {
            //校验ic卡是否属于登录商户
            Card card = cardApi.queryCard(cardChargePlusReq.getCardId(), merchant.getId());
        }

        //年月
        Date month = DateUtil.parseDate(cardChargePlusReq.getMonth(), "yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        //开始时间
        Date startDate = calendar.getTime();

        //结束时间
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.SECOND, -1);
        Date endDate = calendar.getTime();

        //查询充值报表数据
        MchChargeStatisticsRsp mchChargeStatisticsRsp;
        //ic卡维度
        if (!CommonUtil.isNullOrEmpty(cardChargePlusReq.getCardId())) {
            mchChargeStatisticsRsp = dataApi.queryChargeData(cardChargePlusReq.getCardId(), startDate, endDate, true);
        }
        //商户维度
        else {
            mchChargeStatisticsRsp = dataApi.queryMchChargeData(merchant.getId(), startDate, endDate, true);
        }

        //返回
        return new BaseRsp(mchChargeStatisticsRsp);
    }

    /**
     * Description: 按月加油数据查询</br>
     * Date: 2017-08-09 13:44:31</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    @PostMapping(value = "/fuelMonth")
    @ResponseBody
    public Object fuelMonth(HttpSession httpSession, @RequestBody FuelMonthReq fuelMonthReq) {

        //ic卡维度
        if (!CommonUtil.isNullOrEmpty(fuelMonthReq.getCardId()) && CommonUtil.isNullOrEmpty(fuelMonthReq.getTankerId())) {
            CardFuelMonthReq cardFuelMonthReq = new CardFuelMonthReq();
            cardFuelMonthReq.setCardId(fuelMonthReq.getCardId());
            cardFuelMonthReq.setMonth(fuelMonthReq.getMonth());
            return cardFuelMonth(httpSession, cardFuelMonthReq);

        }
        //加油机维度
        else if (CommonUtil.isNullOrEmpty(fuelMonthReq.getCardId()) && !CommonUtil.isNullOrEmpty(fuelMonthReq.getTankerId())) {
            TankerFuelMonthReq tankerFuelMonthReq = new TankerFuelMonthReq();
            tankerFuelMonthReq.setTankerId(fuelMonthReq.getTankerId());
            tankerFuelMonthReq.setMonth(fuelMonthReq.getMonth());
            return tankerFuelMonth(httpSession, tankerFuelMonthReq);
        }
        //商户维度
        else if (CommonUtil.isNullOrEmpty(fuelMonthReq.getCardId()) && CommonUtil.isNullOrEmpty(fuelMonthReq.getTankerId())) {
            MchFuelPlusReq mchFuelPlusReq = new MchFuelPlusReq();
            mchFuelPlusReq.setMonth(fuelMonthReq.getMonth());
            return mchFuelMonth(httpSession, mchFuelPlusReq);
        } else {
            throw new BusinessRuntimeException(ErrorCode.FUEL_DATA);
        }
    }
}
